package com.shop.member.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shop.member.service.MemberService;
import com.shop.member.vo.MemberVO;


/**
 * 회원관련 
 * @author geryon
 *
 */
@Controller
@RequestMapping("/MEMBER")
public class MemberController {

	private final static int LIST_ROW_CNT = 20; // 게시물 리스트 갯수
	
	@Autowired
	private MemberService service;

	/**
	 * 회원 리스트 페이지 호출
	 * 리스트 화면을 호출한다
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/")
	public String requestScreen(Model model) {
		return "member/member_list";
	}

	/**
	 * 회원 리스트
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value="/list")
	public Model listMember(Model model,
							@RequestParam("page_no") int current_page,
							@RequestParam("src_type") String src_type,
							@RequestParam("src_keyword") String src_keyword) {
		
		int tot_cnt = 0; // 전체건수
		int tot_page_cnt = 0; // 총페이지수
		
		// 총 리스트 항목 구하기
		tot_cnt = service.getTotalMemberCount(src_type, src_keyword);
		
		// 총페이지
		tot_page_cnt = tot_cnt / LIST_ROW_CNT + (((tot_cnt % LIST_ROW_CNT) == 0) ? 0:1);
		
		
		List<MemberVO.MemberItem> list = service.listMember(current_page,
															LIST_ROW_CNT,
															src_type,
															src_keyword);
		model.addAttribute("MODEL", list);
		model.addAttribute("TOTAL_COUNT", tot_cnt);
		model.addAttribute("TOTAL_PAGE_COUNT", tot_page_cnt);
		return model;
	}
	
	/**
	 * 회원 등록
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/add")
	public Model addMember(Model model,
							@RequestBody MemberVO.MemberItem memberItem) {
		// 사용자 체크
		int isUserCnt = service.isUserCnt(memberItem.getId(),
										memberItem.getPw());
		
		if (isUserCnt == 0) {
			service.addMember(memberItem);
			model.addAttribute("PROCESS_CODE", true);
		} else {
			model.addAttribute("PROCESS_CODE", false);
		}
		return model;
	}

	/**
	 * 회원 정보 상세조회
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value="/{member_seq}")
	public Model getMember(Model model,
							@PathVariable ("member_seq") String member_seq) {
		
		MemberVO.MemberItem vo = (MemberVO.MemberItem)service.getMember(member_seq);
		model.addAttribute("MODEL", vo);
		return model;
	}

	/**
	 * 회원 수정
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value="/{member_seq}")
	public Model updateMember(Model model,
							@PathVariable ("member_seq") String member_seq,
							@RequestBody MemberVO.MemberItem memberItem) {
		
		if (member_seq.equals(""+memberItem.getSeq())) {
			service.updateMember(memberItem);
			model.addAttribute("PROCESS_CODE", true);
		} else {
			model.addAttribute("PROCESS_CODE", false);
		}
		return model;
	}

	/**
	 * 관리자 삭제
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value="/{member_seq}")
	public Model deleteAdmin(Model model,
							@PathVariable ("member_seq") String member_seq) {
		
		service.deleteMember(member_seq);
		model.addAttribute("PROCESS_CODE", true);
		return model;
	}
	
	/**
	 * 관리자 로그인
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/login")
	public Model loginMember(Model model) {
		
		return model;
	}

	/**
	 * 관리자 로그아웃
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/logout")
	public Model logoutMember(Model model) {
		
		return model;
	}
	
}
